package edu.itstudy.control;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.itstudy.bean.User;
import edu.itstudy.services.IUserServices;
import edu.itstudy.services.impl.IUserServicesImpl;

public class UserIndexServlet extends HttpServlet {
	
private IUserServices iuserServices = new IUserServicesImpl();
	
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
		 * 获取查询条件 
		 * 根据条件获取所有用户信息
		 * 在页面显示
		 */
		request.setCharacterEncoding("UTF-8");
		//如果参数存在无值 返回"";无参 返回null;有参 返回String
		String name = request.getParameter("name");
		String age = request.getParameter("age");
		String toage = request.getParameter("toage");
		String recommend = request.getParameter("recommend");

		if(null == age || "".equals(age)) {
			age= "0";
		}
		if(null == toage || "".equals(toage)) {
			toage= "0";
		}
		if(null == recommend || "".equals(recommend)) {
			recommend = "2";
		}
	
		User user = new User(0,name,Integer.parseInt(age),Integer.parseInt(toage),Integer.parseInt(recommend));
		
		 List<User> list = iuserServices.getAllUser(user);
		 request.setAttribute("list", list);
		 request.setAttribute("user", user);
		request.getRequestDispatcher("/WEB-INF/content/userIndex.jsp").forward(request, response);
	}
}
